<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="安装与配置Mac上使用图形化安装工具、brew、xcode自带的都可以。检查 1git --version  环境配置配置文件为 *gitconfig 作用域有三种：  系统 system (&#x2F;etc&#x2F;gitconfig) 全局（用户）global (在 ~&#x2F;.gitconfig ) 项目 (在仓库的 &#x2F;project&#x2F;.git&#x2F;conf">
<meta property="og:type" content="article">
<meta property="og:title" content="Git的学习笔记">
<meta property="og:url" content="http://example.com/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="安装与配置Mac上使用图形化安装工具、brew、xcode自带的都可以。检查 1git --version  环境配置配置文件为 *gitconfig 作用域有三种：  系统 system (&#x2F;etc&#x2F;gitconfig) 全局（用户）global (在 ~&#x2F;.gitconfig ) 项目 (在仓库的 &#x2F;project&#x2F;.git&#x2F;conf">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-05-07T17:43:28.000Z">
<meta property="article:modified_time" content="2022-10-18T07:52:14.106Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="git">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/","path":"2022/05/08/Git的学习笔记/","title":"Git的学习笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Git的学习笔记 | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE"><span class="nav-number">1.</span> <span class="nav-text">安装与配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="nav-number">1.1.</span> <span class="nav-text">环境配置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">2.</span> <span class="nav-text">基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E5%BA%93%E3%80%81%E5%B7%A5%E4%BD%9C%E5%8C%BA%E3%80%81%E6%9A%82%E5%AD%98%E5%8C%BA"><span class="nav-number">2.1.</span> <span class="nav-text">版本库、工作区、暂存区</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E4%B8%8B%E6%96%87%E4%BB%B6%E7%9A%84%E7%8A%B6%E6%80%81"><span class="nav-number">2.2.</span> <span class="nav-text">git下文件的状态</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gitignore"><span class="nav-number">2.3.</span> <span class="nav-text">.gitignore</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%93%E5%BA%93"><span class="nav-number">2.4.</span> <span class="nav-text">仓库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9A%82%E5%AD%98%E5%8C%BA"><span class="nav-number">2.5.</span> <span class="nav-text">暂存区</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E5%BA%93"><span class="nav-number">2.6.</span> <span class="nav-text">版本库</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93"><span class="nav-number">3.</span> <span class="nav-text">远程仓库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%86%E6%94%AF"><span class="nav-number">4.</span> <span class="nav-text">分支</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%A0%87%E7%AD%BE"><span class="nav-number">5.</span> <span class="nav-text">标签</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E6%8B%BE%E9%81%97"><span class="nav-number">6.</span> <span class="nav-text">命令拾遗</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/05/08/Git%E7%9A%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Git的学习笔记 | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Git的学习笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-08 01:43:28" itemprop="dateCreated datePublished" datetime="2022-05-08T01:43:28+08:00">2022-05-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-10-18 15:52:14" itemprop="dateModified" datetime="2022-10-18T15:52:14+08:00">2022-10-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/DevOps/" itemprop="url" rel="index"><span itemprop="name">DevOps</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="安装与配置"><a href="#安装与配置" class="headerlink" title="安装与配置"></a>安装与配置</h1><p>Mac上使用图形化安装工具、brew、xcode自带的都可以。<br>检查</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git --version</span><br></pre></td></tr></table></figure>

<h2 id="环境配置"><a href="#环境配置" class="headerlink" title="环境配置"></a>环境配置</h2><p>配置文件为 *gitconfig</p>
<p>作用域有三种：</p>
<ol>
<li>系统 system (&#x2F;etc&#x2F;gitconfig)</li>
<li>全局（用户）global (在 ~&#x2F;.gitconfig )</li>
<li>项目 (在仓库的 &#x2F;project&#x2F;.git&#x2F;config)</li>
</ol>
<ul>
<li><p>查看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --list</span><br></pre></td></tr></table></figure>
</li>
<li><p>配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name <span class="string">&#x27;jsy&#x27;</span></span><br><span class="line">git config --global user.email <span class="string">&#x27;153xxxxx@qq.com&#x27;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h1><h2 id="版本库、工作区、暂存区"><a href="#版本库、工作区、暂存区" class="headerlink" title="版本库、工作区、暂存区"></a>版本库、工作区、暂存区</h2><ul>
<li><p>版本库<br>就是那个 .git<br>里面有</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir .git % <span class="built_in">ls</span></span><br><span class="line">HEAD		description	info		refs</span><br><span class="line">config		hooks		objects</span><br></pre></td></tr></table></figure>

</li>
<li><p>工作区<br>我们能看到的那个包含.git的文件夹, 比如下面的那个 repo 文件夹就是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir .git % <span class="built_in">cd</span> ..</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % <span class="built_in">pwd</span></span><br><span class="line">/Users/jisongyang/learn-git-repo/repo</span><br></pre></td></tr></table></figure>
</li>
<li><p>暂存区<br>临时保存的修改的文件的地方</p>
</li>
</ul>
<h2 id="git下文件的状态"><a href="#git下文件的状态" class="headerlink" title="git下文件的状态"></a>git下文件的状态</h2><ol>
<li>untracked 未被git管理</li>
<li>tracked<ol>
<li>unmodified 未修改</li>
<li>modified 已修改</li>
<li>staged 暂存</li>
</ol>
</li>
</ol>
<p>查看状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git status</span><br><span class="line"><span class="comment"># 未跟踪</span></span><br><span class="line">git status -s</span><br></pre></td></tr></table></figure>

<p>举例</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo % clear</span><br><span class="line"><span class="comment"># 未跟踪</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">?? a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git add a.txt</span><br><span class="line"><span class="comment"># 已跟踪</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">A  a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % <span class="built_in">echo</span> hello &gt;&gt; a.txt</span><br><span class="line"><span class="comment"># 已修改</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">AM a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git reset a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line">?? a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git commit a.txt</span><br><span class="line">[master (root-commit) 5273424] init a.txt</span><br><span class="line"> 1 file changed, 2 insertions(+)</span><br><span class="line"> create mode 100644 a.txt</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo % git status -s</span><br><span class="line"><span class="comment"># a.txt 在状态里不可见了</span></span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo %</span><br></pre></td></tr></table></figure>

<h2 id="gitignore"><a href="#gitignore" class="headerlink" title=".gitignore"></a>.gitignore</h2><p>当有文件不需要git管理的时候，使用这个文件就很有必要了。<br>比如日志、项目编译出来的文件、IDEA的本地信息等等，就不需要git管理。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 文件名固定</span></span><br><span class="line"><span class="built_in">touch</span> .gitignore</span><br></pre></td></tr></table></figure>

<p>看例子就好</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 忽略所有 *.class</span></span><br><span class="line">*.class</span><br><span class="line"><span class="comment"># 但是不忽略Hello.class</span></span><br><span class="line">!Hello.class</span><br><span class="line"><span class="comment"># 忽略当前目录下的TODO文件夹</span></span><br><span class="line">/TODO </span><br><span class="line"><span class="comment"># 忽略 target 下的所有文件</span></span><br><span class="line">target/</span><br><span class="line"><span class="comment"># 忽略 doc/*.txt的所有文件夹</span></span><br><span class="line">doc/*.txt</span><br><span class="line"><span class="comment">## 忽略 doc及其所有子目录下所有的 *.txt</span></span><br><span class="line">doc/**/*.txt</span><br></pre></td></tr></table></figure>


<h2 id="仓库"><a href="#仓库" class="headerlink" title="仓库"></a>仓库</h2><ul>
<li>在本地初始化仓库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git init</span><br></pre></td></tr></table></figure>
<p>创建完会多一个 .init文件(默认不可见)</p>
<ul>
<li>从远程仓库克隆<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> repo</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="暂存区"><a href="#暂存区" class="headerlink" title="暂存区"></a>暂存区</h2><p>从工作区到暂存区</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add filename</span><br><span class="line"><span class="comment"># 添加全部文件</span></span><br><span class="line">git add .</span><br></pre></td></tr></table></figure>
<p>暂存区的目录树会被重写，被 master 分支指向的目录树所替换，但是工作区不受影响</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset HEAD</span><br></pre></td></tr></table></figure>

<p>直接从暂存区删除文件，工作区则不做出改变</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">rm</span> --cached -f &lt;file&gt;</span><br></pre></td></tr></table></figure>


<h2 id="版本库"><a href="#版本库" class="headerlink" title="版本库"></a>版本库</h2><ul>
<li><p>从暂存区到版本库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m <span class="string">&#x27;message&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改上一次的commit</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit --amend</span><br></pre></td></tr></table></figure>
<p>具体使用方法见<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/100243017">git commit –amend 修改git提交记录用法详解</a></p>
</li>
</ul>
<h1 id="远程仓库"><a href="#远程仓库" class="headerlink" title="远程仓库"></a>远程仓库</h1><p><strong>查看已连接的远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git remote</span><br><span class="line">git remote -v</span><br><span class="line">git remote show origin</span><br></pre></td></tr></table></figure>

<p><strong>添加远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add [shortname] url</span><br></pre></td></tr></table></figure>

<p><strong>从远程仓库克隆到本地</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> repo</span><br></pre></td></tr></table></figure>

<p><strong>移除无效的远程仓库</strong>（只是移除关联关系，不会对远程仓库有影响）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote <span class="built_in">rm</span> origin</span><br></pre></td></tr></table></figure>

<p><strong>抓取(fetch)</strong><br>从远程仓库将最新版本获取到本地仓库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br></pre></td></tr></table></figure>

<p><strong>拉取(pull)</strong><br>相当于fetch + merge : 从远程仓库将最新版本获取到本地仓库，并merge<br>（根据配置也可能是相当于 fetch + rebase）<br>(可能报 merge unrelated histories)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull [romote-name] [branch-name]</span><br></pre></td></tr></table></figure>

<p><strong>推送(push)</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push [romote-name] [branch-name]</span><br></pre></td></tr></table></figure>



<h1 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h1><p>master分支是git自动创建的，但没并没有什么特殊之处。</p>
<p><strong>查看分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列出所有本地分支</span></span><br><span class="line">git branch</span><br><span class="line"><span class="comment"># 列出所有远程分支</span></span><br><span class="line">git branch -r</span><br><span class="line"><span class="comment"># 列出远程和本地分支</span></span><br><span class="line">git branch -a</span><br></pre></td></tr></table></figure>

<p><strong>创建分支</strong>(在原先分支下创建分支，内容会初始共享)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch branch-name</span><br></pre></td></tr></table></figure>
<p>举例（*master表示当前在master分支下）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch b1</span><br><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch</span><br><span class="line">  b1</span><br><span class="line">* master</span><br></pre></td></tr></table></figure>


<p><strong>切换分支</strong>（这个时候就自动把目标分支在本地仓库里的文件换到工作区里）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git checkout b1</span><br><span class="line"></span><br><span class="line"># &quot;打开&quot;远程仓库的分支</span><br><span class="line"># 如果没有全部拉下来的话</span><br><span class="line"># git fetch -all</span><br><span class="line">git checkout -b dev（本地分支名） origin/dev（远程分支名）</span><br></pre></td></tr></table></figure>

<p>两个命令合并到一步：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b dev</span><br></pre></td></tr></table></figure>


<p><strong>将本地分支推送至远程仓库</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u origin b1</span><br></pre></td></tr></table></figure>

<p><strong>分支合并</strong><br>(可能会冲突)<br>这里的冲突需要正确理解（这里的冲突的就像是乐观锁的加版本号一样，事实上在svn里确实有全局版本号，不允许脏写发生）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 把b1分支合并到当前的master分支</span></span><br><span class="line">git merge b1</span><br></pre></td></tr></table></figure>

<p><strong>rebase</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git fetch</span><br><span class="line">git rebase dev</span><br></pre></td></tr></table></figure>

<p><strong>是rebase还是merge？</strong><br>git rebase和git merge这两个命令都旨在将更改代码从一个分支合并到另一个分支，但二者的合并方式却有很大的不同。<br>git merge优点是分支代码合并后不破坏原分支的代码提交记录，缺点就是会产生额外的提交记录并进行两条分支的合并，<br>git rebase 优点是无须新增提交记录到目标分支，rebase后可以将对象分支的提交历史续上目标分支上，形成线性提交历史记录，进行review的时候更加直观。</p>
<blockquote>
<p><strong>git rebase的黄金原则</strong><br><strong>不能对一个共享的分支进行Git rebase操作</strong>。<br><del>比如，现在在feature分支上，现在想要合并master分支的代码：<br>也就是说，如果master分支是共享的（这是一种很常见的方式），<br>那么就不能进行 git rebase master，<br>而是要进行 git merge master。</del> </p>
</blockquote>
<p>总结</p>
<ul>
<li><strong>融合代码到公共分支的时使用git merge</strong>，而不用git rebase</li>
<li><strong>融合代码到个人分支的时候使用git rebase</strong>，可以不污染分支的提交记录，形成简洁的线性提交历史记录。<br>参考 <a target="_blank" rel="noopener" href="https://joyohub.com/2020/04/06/git-rebase/">git rebase和git merge有什么区别？</a></li>
</ul>
<p><strong>删除分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除的是本地的分支，对远程的分支无关</span></span><br><span class="line">git branch -d branch-name</span><br><span class="line"><span class="comment"># 强制删除（git的保护，以防误删）</span></span><br><span class="line">git branch -D branch-name</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jisongyang@SongyangJi-MacBookAir repo1 % git branch -d b1</span><br><span class="line">error: The branch <span class="string">&#x27;b1&#x27;</span> is not fully merged.</span><br><span class="line">If you are sure you want to delete it, run <span class="string">&#x27;git branch -D b1&#x27;</span>.</span><br></pre></td></tr></table></figure>

<p><strong>删除远程分支</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin -d branchname</span><br></pre></td></tr></table></figure>




<h1 id="标签"><a href="#标签" class="headerlink" title="标签"></a>标签</h1><p>Git给历史中的某次提交打上标签，以示重要。<br>标签指的是某个分支某个特定时间节点的状态。（就像是打了一份快照）</p>
<p><strong>查看已有标签</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列出已有标签</span></span><br><span class="line">git tag</span><br><span class="line">git show tag</span><br></pre></td></tr></table></figure>
<p><strong>创建标签</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git tag tagname</span><br><span class="line">git tag tagname -m message</span><br><span class="line"><span class="comment"># 举例</span></span><br><span class="line">git tag v1.0 -m <span class="string">&#x27;finally a stable release&#x27;</span></span><br></pre></td></tr></table></figure>

<p><strong>将标签推送到远程仓库</strong>：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 推送某个标签</span></span><br><span class="line">git push origin vxx</span><br><span class="line"><span class="comment"># 推送所有不在remote的标签</span></span><br><span class="line">git push origin --tags</span><br></pre></td></tr></table></figure>

<p>实操</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 拉取所有版本</span></span><br><span class="line">git fetch --all --tags</span><br><span class="line"><span class="comment"># 查看有哪些版本</span></span><br><span class="line">git tag</span><br><span class="line"><span class="comment"># 创建某个版本的新的分支并切换过去</span></span><br><span class="line">git checkout tags/v1.0 -b v1.0-branch</span><br></pre></td></tr></table></figure>


<p><a target="_blank" rel="noopener" href="https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE">Git-基础-打标签</a></p>
<h1 id="命令拾遗"><a href="#命令拾遗" class="headerlink" title="命令拾遗"></a>命令拾遗</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看工作区和暂存区的文件</span></span><br><span class="line">git ls-files</span><br></pre></td></tr></table></figure>

<p>删除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">rm</span> file</span><br></pre></td></tr></table></figure>
<p><code>git rm</code>和<code>rm</code>的区别很多，网上讲的也都是其中的一个方面，不细讲了，最好自己试试。</p>
<p>查看日志</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/git/" rel="tag"># git</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/05/07/Sdu-Devops-02/" rel="prev" title="使用Jenkins的pipeline实现CI/CD">
                  <i class="fa fa-chevron-left"></i> 使用Jenkins的pipeline实现CI/CD
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/05/08/%E4%BD%BF%E7%94%A8Docker%E7%9A%84API%E5%8F%8ASDK/" rel="next" title="使用Docker的API及SDK">
                  使用Docker的API及SDK <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
